<template>
  <component
    v-if="businessType"
    :is="compForm"
    :business-type="businessType"
    :rollbacks="rollbacks"
    :forward-user="forwardUser"
    :node-info="nodeInfo"
    :ticket-info="ticketInfo"
    :ticket-id="ticketId"
    :node-task-id="nodeTaskId"
    :dict-review-result="dictReviewResult"
    :argeeable="argeeable"
    @success="onSuccess"
  />
</template>

<script>
import Technology from './components/Technology.vue'
import MeasuresNode from './components/MeasuresNode.vue'
import AcceptDisclosureForm from './components/AcceptDisclosureForm.vue'
import DisclosureForm from './components/DisclosureForm.vue'
import GuardianshipForm from './components/GuardianshipForm.vue'
import AnalyseForm from './components/AnalyseForm.vue'
import CommonInspectForm from './components/CommonInspectForm.vue'
import { actNodePermissionEnum } from '@/utils/constant.js'
import { getCurrentInspectDetail, getTicketNodeUsers } from '@/api/spw/ticketInspect.js'
import { getTicketInfoById } from '@/api/spw/ticketApply.js'
import { mapGetters } from 'vuex'
import { nodeTypeFlagEnum } from '@/utils/constant.js'
export default {
  components: {
    Technology,
    MeasuresNode,
    DisclosureForm,
    AcceptDisclosureForm,
    CommonInspectForm,
    GuardianshipForm,
    AnalyseForm
  },
  computed: {
    ...mapGetters(['staffId']),
    compForm() {
      switch (this.nodeInfo.nodeTypeFlag) {
        // 分析
        case nodeTypeFlagEnum.ANALYSE:
          return 'AnalyseForm'
        // 措施
        case nodeTypeFlagEnum.MEASURE:
          return 'MeasuresNode'
        // 交底
        case nodeTypeFlagEnum.DISCLOSE:
          return 'DisclosureForm'
        // 接受交底
        case nodeTypeFlagEnum.ACCEPT_DISCLOSURE:
          return 'AcceptDisclosureForm'
        // 监护
        case nodeTypeFlagEnum.JIANHU:
          return 'GuardianshipForm'
        // 工艺交出
        case nodeTypeFlagEnum.TECHNOLOGY:
          return 'Technology'
        default:
          return 'CommonInspectForm'
      }
    },
    businessType() {
      return this.ticketInfo.workTypeFlag
    }
  },
  async onLoad(option) {
    const { nodeTaskId, ticketId } = option
    this.nodeTaskId = nodeTaskId
    this.ticketId = ticketId
    const [res, ticketRes] = await Promise.all([getCurrentInspectDetail(nodeTaskId), getTicketInfoById(ticketId)])
    const nodeUserRes = await getTicketNodeUsers(ticketId, res.data.nodeId)
    this.rollbacks = []
    this.forwardUser = nodeUserRes.data.filter((pet) => Number(pet.maintainer) !== 1 && Number(pet.active) !== 1)
    this.nodeInfo = res.data
    this.ticketInfo = ticketRes.data
    uni.setNavigationBarTitle({
      title: this.nodeInfo.nodeFormTitle || this.nodeInfo.nodeName
    })
    const canForwardUser = nodeUserRes.data.find((pet) => Number(pet.maintainer) === 1 && pet.userId === this.staffId)

    const result = []
    res.data.nodePermissions.forEach((item) => {
      item.optAlias = item.optAlias || item.permissionLabel
      // 转发/去掉暂停
      if (item.permissionType === actNodePermissionEnum.FORWARD) {
        if (canForwardUser) {
          result.push(item)
        }
      } else if (item.permissionType !== actNodePermissionEnum.PAUSE) {
        result.push(item)
      }

      if (item.permissionType === actNodePermissionEnum.AGREE) {
        this.argeeable = true
      }
    })
    this.dictReviewResult = result
  },
  data() {
    return {
      nodeInfo: {},
      ticketId: '',
      ticketInfo: {},
      nodeTaskId: '',
      dictReviewResult: [],
      argeeable: false,
      rollbacks: [],
      forwardUser: []
    }
  },
  methods: {
    onSuccess() {
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.emit('callback:success', {
        data: true
      })
      uni.navigateBack()
    }
  }
}
</script>

<style></style>
